package com.mingtu.jgspatrol.receiver;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.mingtu.jgspatrol.activity.LockScreenNewActivity;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2024-7-1 17:02:38
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public class LockScreenBroadcastReceiver2 extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null && action.length() > 0) {
            switch (action) {
                //当屏幕灭了
                case Intent.ACTION_SCREEN_OFF:
//                                try {
//                                    PowerManager powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
//                                    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyTag"); //保持CPU 运转，屏幕和键盘灯有可能是关闭的
//                                    wakeLock.acquire();
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
                    break;
                //当屏幕亮了
                case Intent.ACTION_SCREEN_ON:
                    //加载慢
                    //跳转添加标记
//                                Intent i = new Intent(context, LockScreenActivity.class);
//                                i.setPackage(AppUtils.getAppPackageName());
//                                i.setAction("com.android.lockscreen");
//                                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//                                startActivity(i);

                    //关闭系统锁屏 有无法重启锁屏的bug
//                                try {
//                                    KeyguardManager keyguardManager = (KeyguardManager) getActivity().getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
//                                    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
//                                    keyguardLock.disableKeyguard();
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }

//                                if (wakeLock != null) {
//                                    wakeLock.release();
//                                }

//                    MyLogUtil.e("ComponentStatus", "Activity available: " + isActivityAvailable(context, LockScreenNewActivity.class));


                    //系统不会重复创建锁屏页，所以这里不用做相关判断
                    Intent i = new Intent(ActivityUtils.getTopActivity(), LockScreenNewActivity.class);
                    i.setPackage(AppUtils.getAppPackageName());
                    i.setAction("com.android.lockscreen");
                    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                            | Intent.FLAG_FROM_BACKGROUND
                            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                            | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
                            | Intent.FLAG_ACTIVITY_NO_ANIMATION
                            | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
                            | Intent.FLAG_ACTIVITY_NEW_TASK);
                    ActivityUtils.getTopActivity().startActivity(i);
//                    PendingIntent pendingIntent;
//                    // 判断当前设备的API级别是否为Android 12（API 31）或更高
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Android S 对应 API 级别 31
//                        // 如果是，则根据需求选择 FLAG_MUTABLE 或 FLAG_IMMUTABLE
//                        // 示例中使用 FLAG_IMMUTABLE 以提高安全性
//                        pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_REQUEST_CODE_LOCK_SCREEN, intent, PendingIntent.FLAG_MUTABLE);
//                    } else {
//                        // 在旧版本的Android上，不需要指定可变性标志
//                        pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_REQUEST_CODE_LOCK_SCREEN, intent, 0);
//                    }
//                    try {
//                        pendingIntent.send(PENDING_INTENT_REQUEST_CODE_LOCK_SCREEN);
//                    } catch (PendingIntent.CanceledException e) {
//                        e.printStackTrace();
//                    }

                    break;
                default:
                    break;
            }
        }
    }

    public boolean isActivityAvailable(Context context, Class<?> cls) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(context, cls);
        ComponentName componentName = intent.getComponent();
        if (componentName == null) {
            return false;
        }
        // 检查组件是否存在
        return packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null;
    }
}
